package com.mall.db.api;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.SmsHomeRecommendSubjectMapper;
import com.mall.pojo.SmsHomeNewProduct;
import com.mall.pojo.SmsHomeRecommendSubject;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

@DubboService
public class SmsHomeRecommendSubjectApiImpl implements SmsHomeRecommendSubjectApi{

    @Autowired
    private SmsHomeRecommendSubjectMapper homeRecommendSubjectMapper;

    /**
     * 查询推荐专题分页信息
     * @param pageNum
     * @param pageSize
     * @param subjectName
     * @param recommendStatus
     * @return
     */
    @Override
    public Page<SmsHomeRecommendSubject> list(Integer pageNum, Integer pageSize, String subjectName, Integer recommendStatus) {
        Page<SmsHomeRecommendSubject> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsHomeRecommendSubject> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ObjectUtil.isNotEmpty(subjectName), SmsHomeRecommendSubject::getSubjectName, subjectName)
                .eq(ObjectUtil.isNotEmpty(recommendStatus), SmsHomeRecommendSubject::getRecommendStatus, recommendStatus)
                .orderByDesc(SmsHomeRecommendSubject::getSort);
        return homeRecommendSubjectMapper.selectPage(page,wrapper);
    }

    /**
     * 根据一组id查询推荐专题信息
     * @param ids
     * @return
     */
    @Override
    public List<SmsHomeRecommendSubject> findProductByIds(Long[] ids) {
        LambdaQueryWrapper<SmsHomeRecommendSubject> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SmsHomeRecommendSubject::getId,ids);
        return homeRecommendSubjectMapper.selectList(wrapper);
    }

    /**
     * 修改推荐专题状态
     * @param subject
     * @return
     */
    @Override
    public Integer updateSubject(SmsHomeRecommendSubject subject) {
        int count = homeRecommendSubjectMapper.updateById(subject);
        return count;
    }

    /**
     * 根据商品id查询推荐专题信息
     * @param subjectId
     * @return
     */
    @Override
    public SmsHomeRecommendSubject findSubjectBySubjectId(Long subjectId) {
        LambdaQueryWrapper<SmsHomeRecommendSubject> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SmsHomeRecommendSubject::getSubjectId,subjectId);
        return homeRecommendSubjectMapper.selectOne(wrapper);
    }

    /**
     * 添加推荐专题
     * @param subject
     */
    @Override
    public void create(SmsHomeRecommendSubject subject) {
        homeRecommendSubjectMapper.insert(subject);
    }

    /**
     * 根据id查询推荐专题信息
     * @param id
     * @return
     */
    @Override
    public SmsHomeRecommendSubject findSubjectById(Integer id) {
        return homeRecommendSubjectMapper.selectById(id);
    }

    /**
     * 批量删除首页推荐专题
     * @param ids
     * @return
     */
    @Override
    public Integer deleteByIds(Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        return homeRecommendSubjectMapper.deleteBatchIds(idList);
    }
}
